/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Database;

import javax.xml.parsers.*;
import org.w3c.dom.*;

/**
 *
 * @author Marco
 */
public class xmlUsersDatabase {
    Document doc;
    int i;
    /**
     * Construct document for XML
     */
    public xmlUsersDatabase(){
        try{
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            doc = docBuilder.parse("users.xml");
        }catch(Exception e){
            System.out.println(e);
        }
    }
    public boolean verifyUser(String username, String passwd){
        boolean result = false;
        String verified = "n";
        String dbuser;
        String dbpasswd;
        NodeList nl = doc.getElementsByTagName("user");
        doc.normalize();
        while(verified.equals("n")){
                for(i=0;i<nl.getLength();i++){
                    Element userelement = (Element) nl.item(i);
                    dbuser = userelement.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
                    dbpasswd = userelement.getElementsByTagName("passwd").item(0).getFirstChild().getNodeValue();
                    if(username.equals(dbuser) && passwd.equals(dbpasswd)){
                        result = true;
                        verified = "y";
                    }
                }
        }
        return result;
    }

}
